﻿' LibWowArmory
' by Ronald M. Clifford (roncli@roncli.com)
'
' This source code is released under the GNU Library General Public License (LGPL) Version 2.1.

Imports System.Collections.ObjectModel
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Xml.Serialization

Namespace roncliProductions.LibWowArmory.Dungeon.Strings

    ''' <summary>
    ''' A class that caches additional data for the dungons and bosses available on the Armory
    ''' </summary>
    ''' <remarks>This class is largely used for internal purposes</remarks>
    Public Class DungeonStrings
        Inherits WowArmoryData

        Private dssPage As New Schema.page

        ''' <summary>
        ''' Returns a list of available dungeons
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property Dungeons As Collection(Of Dungeon)
            Get
                If dssPage Is Nothing Then
                    Return New Collection(Of Dungeon)
                End If
                Return (From d In dssPage.dungeons.SelectMany(Function(d) d.dungeon)
                        Select New Dungeon(d.id, d.key, d.name, CType(d.release, Enums.ReleaseType),
                                           (From b In d.boss Select New Boss(b.id, b.name)).ToCollection)).ToCollection
            End Get
        End Property

        ''' <summary>
        ''' The URL of the dungeon and boss data
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property URL As String
            Get
                Return "http://www.wowarmory.com/data/dungeonStrings.xml"
            End Get
        End Property

        ''' <summary>
        ''' Indicates to the inherited class that this request does not require login
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Overrides ReadOnly Property RequiresLogin As Boolean
            Get
                Return False
            End Get
        End Property

        ''' <summary>
        ''' Constructor that retrieves the dungeon and boss data from either the armory or the local cache
        ''' </summary>
        ''' <remarks></remarks>
        Public Sub New()
            If Cache.Cache("LibWowArmory.DungeonStrings") Is Nothing Then
                MyBase.Retrieve()

                Dim xsSerializer As New XmlSerializer(dssPage.GetType)

                Using srXML As New StringReader(MyBase.Data)
                    dssPage = CType(xsSerializer.Deserialize(srXML), Schema.page)
                End Using

                Cache.Cache.Insert("LibWowArmory.DungeonStrings", dssPage, Nothing, Date.UtcNow.AddDays(1), Nothing, Caching.CacheItemPriority.NotRemovable, Nothing)
            Else
                dssPage = CType(Cache.Cache("LibWowArmory.DungeonStrings"), Schema.page)
            End If
        End Sub

    End Class

End Namespace